package com.kutuzov.spring4.boot.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.kutuzov.spring4.boot.entity.User;
import com.kutuzov.spring4.boot.service.UserService;

@RestController
public class UserController {
	@Autowired
	private UserService userService;
	
	// 可以配置多个映射路径
	@RequestMapping(value={"/", "/index.html"})
	public ModelAndView loginPage() {
		return new ModelAndView("login");
	}
	
	@RequestMapping(value="/loginCheck.html")
	public ModelAndView loginCheck(HttpServletRequest request, User user) {
		User userExist = userService.getUser(user);
		if(userExist == null) {
			return new ModelAndView("login", "error", "用户名或者密码错误.");
		} else {
//			request.getSession().setAttribute("user", userExist);
			ModelAndView mv = new ModelAndView("main");
			Map<String, Object> modelMap = new HashMap<String, Object>();
			modelMap.put("user", userExist);
			mv.addAllObjects(modelMap);
			return mv;
		}
	}
}
